package pers.cz.encrypt;

/**
 * @program: Reids
 * @description: HByte
 * @author: Cheng Zhi
 * @create: 2022-03-10 10:54
 **/
public class HByte {
    private char abyte;
    HByte.DES3Byte ibyte = new HByte.DES3Byte();

    public HByte() {
    }

    public byte getAbyte() {
        int[] array = new int[]{this.ibyte.bit0, this.ibyte.bit1, this.ibyte.bit2, this.ibyte.bit3, this.ibyte.bit4, this.ibyte.bit5, this.ibyte.bit6, this.ibyte.bit7};
        String bString = "";

        for(int i = 0; i < array.length; ++i) {
            bString = array[i] + bString;
        }

        int j = Integer.valueOf(bString, 2);
        return getByteFromChar((char)j);
        //return 0;
    }

    public static char getBinaryValue(String bString) {
        int result = 0;
        String temp = bString;

        for(int i = 0; i < 32 - bString.length(); ++i) {
            temp = "0" + temp;
        }

        result = result + Integer.valueOf(temp, 2);
        return (char)result;
    }

    public static byte getByteFromChar(char c) {
        if (c >= 128) {
            int t = ~c + 1;
            String bString = Integer.toBinaryString(t);
            bString = bString.substring(bString.length() - 8);
            int temp = Integer.valueOf(bString, 2) * -1;
            return (byte)temp;
        } else {
            return (byte)c;
        }
    }

    public static char getChar(byte b) {
        if (b >= 0) {
            return (char)b;
        } else {
            char temp = (char)(b * -1);
            if (b == -128) {
                return temp;
            } else {
                temp = (char)(~temp + 1);
                String bString = Integer.toBinaryString(temp);
                return getBinaryValue(bString.substring(bString.length() - 8));
            }
        }
    }

    public void setAbyte(byte abyte) {
        String bString = null;
        this.abyte = getChar(abyte);
        if (this.abyte >= 128) {
            bString = Integer.toBinaryString(this.abyte);
        } else {
            bString = Integer.toString(abyte, 2);
            int len = bString.length();

            for(int i = len; i < 8; ++i) {
                bString = "0" + bString;
            }
        }

        this.ibyte.bit7 = (char)Integer.parseInt(bString.charAt(0) + "");
        this.ibyte.bit6 = (char)Integer.parseInt(bString.charAt(1) + "");
        this.ibyte.bit5 = (char)Integer.parseInt(bString.charAt(2) + "");
        this.ibyte.bit4 = (char)Integer.parseInt(bString.charAt(3) + "");
        this.ibyte.bit3 = (char)Integer.parseInt(bString.charAt(4) + "");
        this.ibyte.bit2 = (char)Integer.parseInt(bString.charAt(5) + "");
        this.ibyte.bit1 = (char)Integer.parseInt(bString.charAt(6) + "");
        this.ibyte.bit0 = (char)Integer.parseInt(bString.charAt(7) + "");
    }

    public static class DES3Byte {
        public char bit0;
        public char bit1;
        public char bit2;
        public char bit3;
        public char bit4;
        public char bit5;
        public char bit6;
        public char bit7;

        public DES3Byte() {
        }
    }
}
